package m.toolkit.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class PoiExcelTest {

	private static int version2003 = 2003;
	private static int version2007 = 2003;
	private static int version = version2003;
	private static Workbook wb;
	private static Sheet sheet;
	private static Row row;
	private static Cell cell;

	public static void main(String[] args) throws Exception {
		readExcel("c:\\c.xls");
	}

	public static void readExcel(String excelFilePath) throws Exception {
		InputStream stream = new FileInputStream(new File(excelFilePath));
		Workbook wb = new HSSFWorkbook(stream);

		sheet = wb.getSheetAt(0);
		int rows = sheet.getLastRowNum();
		int cells = sheet.getRow(0).getPhysicalNumberOfCells();
		for (int i = 0; i < rows; i++) {
			row = sheet.getRow(i + 1);

			for (int j = 0; j < cells; j++) {
				cell = row.getCell(j);
				Object obj = getCellValue(cell);
			}
		}
	}

	public static Object getCellValue(Cell cell) {
		switch(cell.getCellType()){
		case Cell.CELL_TYPE_BLANK:
			return "";
		case Cell.CELL_TYPE_NUMERIC:
			return cell.getNumericCellValue();
		case Cell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		case Cell.CELL_TYPE_FORMULA:
			return "";
		}
		return null;
	}
}
